﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Admin.Core.Service.sys.Dict.Dto/ImportOutput 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       ImportOutput
* 创建时间：  2025/6/4 11:03:49
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Filters;
using Magicodes.ExporterAndImporter.Core.Models;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.IE.Core;
using NPOI.SS.UserModel;
using OfficeOpenXml.Table;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Admin.Core.Service.sys.Dict.Dto
{

    /// <summary>
    /// 导入信息输出
    /// </summary>
    public class ImportOutput
    {
        /// <summary>
        /// 数据总数
        /// </summary>
        public long Total { get; set; } = 0;

        /// <summary>
        /// 新增数
        /// </summary>
        public long InsertCount { get; set; } = 0;

        /// <summary>
        /// 更新数
        /// </summary>
        public long UpdateCount { get; set; } = 0;
    }
    /// <summary>
    /// 字典导入列头筛选器
    /// </summary>
    public class DictImportHeaderFilter : IImportHeaderFilter
    {
        public List<ImporterHeaderInfo> Filter(List<ImporterHeaderInfo> importerHeaderInfos)
        {
            foreach (var item in importerHeaderInfos)
            {
                if (item.PropertyName == "Enabled")
                {
                    item.MappingValues = new Dictionary<string, dynamic>()
                    {
                        {"是", true },
                        {"否", false }
                    };
                }
            }
            return importerHeaderInfos;
        }
    }
    /// <summary>
    /// 字典导入
    /// </summary>
    [ExcelImporter(IsLabelingError = true, ImportHeaderFilter = typeof(DictImportHeaderFilter))]
    public class DictImport
    {
        /// <summary>
        /// 字典Id
        /// </summary>
        [ImporterHeader(Name = "字典Id", IsIgnore = true)]
        public long Id { get; set; }

        /// <summary>
        /// 字典类型Id
        /// </summary>
        [ImporterHeader(Name = "字典类型Id", IsIgnore = true)]
        public long DictTypeId { get; set; }

        [ImporterHeader(Name = "字典分类")]
        [Required(ErrorMessage = "不能为空")]
        public string DictTypeName { get; set; }

        [ImporterHeader(Name = "字典名称", IsAllowRepeat = false)]
        [Required(ErrorMessage = "不能为空")]
        public string Name { get; set; }

        [ImporterHeader(Name = "字典编码", IsAllowRepeat = false)]
        public string Code { get; set; }

        [ImporterHeader(Name = "字典值", IsAllowRepeat = false)]
        public string Value { get; set; }

        [ImporterHeader(Name = "启用", IsInterValidation = false)]
        public bool Enabled { get; set; }

        [ImporterHeader(Name = "排序")]
        public int Sort { get; set; }

        [ImporterHeader(Name = "说明")]
        public string Description { get; set; }
    }


    /// <summary>
    /// 布尔值映射
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class BoolValueMappingAttribute : ValueMappingsBaseAttribute
    {
        public override Dictionary<string, object> GetMappings(PropertyInfo propertyInfo)
        {
            var res = new Dictionary<string, object>
        {
            { "是", true },
            { "否", false }
        };
            return res;
        }
    }

    [ExcelExporter(Name = "字典列表")]
    public class DictExportHeader
    {
        [ExporterHeader(DisplayName = "字典分类", ColumnIndex = 2)]
        public string DictTypeName { get; set; }

        [ExporterHeader(DisplayName = "字典名称", ColumnIndex = 3)]
        public string Name { get; set; }

        [ExporterHeader(DisplayName = "字典编码", ColumnIndex = 4)]
        public string Code { get; set; }

        [ExporterHeader(DisplayName = "字典值", ColumnIndex = 5)]
        public string Value { get; set; }

        //[BoolValueMapping]
        //[ValueMapping(text: "是", true)]
        //[ValueMapping(text: "否", false)]
        [ExporterHeader(DisplayName = "启用", ColumnIndex = 6)]
        public bool Enabled { get; set; }

        [ExporterHeader(DisplayName = "排序", ColumnIndex = 7)]
        public int Sort { get; set; }

        [ExporterHeader(DisplayName = "说明", ColumnIndex = 8)]
        public string Description { get; set; }
    }

    [ExcelExporter(Name = "字典列表", TableStyle = TableStyles.Light9, AutoFitAllColumn = true, AutoFitMaxRows = 5000)]
    public class DictExport : DictExportHeader
    {
        [ExporterHeader(DisplayName = "字典编号", Format = "0", ColumnIndex = 1)]
        public long Id { get; set; }

        [ExporterHeader(DisplayName = "创建人员", ColumnIndex = 10)]
        public string CreatedUserRealName { get; set; }

        [ExporterHeader(DisplayName = "创建时间", Format = "yyyy-MM-dd HH:mm:ss", Width = 20, ColumnIndex = 11)]
        public DateTime? CreatedTime { get; set; }

        [ExporterHeader(DisplayName = "修改人员", ColumnIndex = 12)]
        public string ModifiedUserRealName { get; set; }

        [ExporterHeader(DisplayName = "修改时间", Format = "yyyy-MM-dd HH:mm:ss", Width = 20, ColumnIndex = 13)]
        public virtual DateTime? ModifiedTime { get; set; }
    }
}
